package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 *
 */
@RestController
@RequestMapping("/admin/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private DtsAdminService dtsAdminService;
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")
    @RequiresPermissions(value = {"admin:admin:list"})
    @GetMapping("/list")
    public Object adminList(String username,@RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "20") Integer limit
            ,@RequestParam(defaultValue = "add_time") String sort
            ,@RequestParam(defaultValue = "desc") String order){
        IPage<DtsAdmin> dtsAdminIPage = dtsAdminService.selectByPage(username,page,limit,sort,order);
        long total = dtsAdminIPage.getTotal();
        List<DtsAdmin> records = dtsAdminIPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items",records);


        return ResponseUtil.ok(map);
    }
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "修改")
    @RequiresPermissions(value = {"admin:admin:update"})
    @PostMapping("/update")
    public Object updateAdmin(@RequestBody DtsAdmin dtsAdmin){

        dtsAdminService.update(dtsAdmin);

        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "添加")
    @RequiresPermissions(value = {"admin:admin:create"})
    @PostMapping("/create")
    public Object createAdmin(@RequestBody DtsAdmin dtsAdmin){

        dtsAdminService.insert(dtsAdmin);

        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "删除")
    @RequiresPermissions(value = {"admin:admin:delete"})
    @PostMapping("/delete")
    public Object deleteAdmin(@RequestBody DtsAdmin dtsAdmin){

        dtsAdminService.delete(dtsAdmin);

        return ResponseUtil.ok();
    }



}
